/*
 * TODO: Description
 *
 * Copyright (C) 2010 Johan Waldeback
 *  
 * This file is part of the OpenFMS project (http://www.openfms-project.org)
 * 
 * OpenFMS is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#ifndef __OfmsSimpleCar_H__
#define __OfmsSimpleCar_H__

#include "InputDevice.h"
#include "ModelSimulator.h"
#include "BulletTickListener.h"

#include <OgreSceneManager.h>
#include <OgreEntity.h>
#include <OgreBulletDynamicsWorld.h>
#include <Shapes/OgreBulletCollisionsBoxShape.h>
#include <Shapes/OgreBulletCollisionsCylinderShape.h>
//btGeneric6DofConstraint.h


namespace OpenFms
{

/**
 * Vehicle class to handle (all) FMS-ish files
 */
	class OfmsSimpleCar : public BulletTickListener
{
private:
	InputDevice *m_pInputDevice;   /**< Points to an Input Device, the model can use to read input */
	Ogre::SceneNode *m_pSceneNode; /**< Points to an Ogre SceneNode to which this object is attached */

	Ogre::Entity *m_pEntity; /**< Pointer to the loaded Ogre::Entity  */
	OgreBulletCollisions::BoxCollisionShape *m_pCollisionShape;
	OgreBulletDynamics::RigidBody *m_pRigidBody;

	OgreBulletCollisions::CylinderCollisionShape *m_pWheelShape;
	OgreBulletDynamics::RigidBody *m_pFrontRightWheel;
	OgreBulletDynamics::RigidBody *m_pFrontLeftWheel;
	OgreBulletDynamics::RigidBody *m_pRearRightWheel;
	OgreBulletDynamics::RigidBody *m_pRearLeftWheel;
	btGeneric6DofConstraint *m_pFrontRightWheelConstraint;
	btGeneric6DofConstraint *m_pFrontLeftWheelConstraint;
	btHingeConstraint *m_pRearRightWheelConstraint;
	btHingeConstraint *m_pRearLeftWheelConstraint;

	ModelSimulator_environment_t m_environment;
	ModelSimulator_inertias_t m_inertias; 

	static const size_t m_nChannels = 4;
	void captureInput();

	bool findParName( Ogre::String &result, const Ogre::String &group );
	bool findMeshName( Ogre::String &result, const Ogre::String &group );
	bool findMaterialName( Ogre::String &result, const Ogre::String &group );
	bool loadSimulator( const TCHAR *pModuleName, const Ogre::String &resourceName, const Ogre::String &groupName );

	static size_t m_uniqueCount;
	Ogre::String generateUniqueName();

public:
	OfmsSimpleCar();
	~OfmsSimpleCar();
	bool load( const Ogre::String &group, OgreBulletDynamics::DynamicsWorld *pWorld, Ogre::SceneManager *pSceneManager );
	void unload();

	void attachInput( InputDevice *pInput );
	void detachInput();

	Ogre::SceneNode *getSceneNode() const;

	void onTick(const btDynamicsWorld *world, btScalar timeStep);
	void onFrameStarted(const Ogre::FrameEvent &evt);
    
};

} // namespace OpenFms
#endif // __OfmsSimpleCar_H__


